package app.frames;


import app.actions.AddSeminarAction;
import app.actions.AddStudentAction;
import app.actions.AddSubjectAction;
import app.actions.ChangeLanguageAction;
import app.actions.DatabaseAction;
import app.actions.ExitAction;
import app.actions.MassAttendanceChangeAction;
import app.actions.RemoveStudentAction;
import app.actions.RegisterStudentAction;
import app.actions.RemoveAllSeminarsAction;
import app.actions.RemoveSeminarAction;
import app.actions.RemoveSubjectAction;
import app.actions.UnregisterStudentAction;
import app.gui.JDate;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.tree.TreePath;
import libs.Attendance;
import libs.AttendanceEntry;
import libs.AttendanceException;
import libs.AttendanceImplDb;
import libs.Seminar;
import libs.Student;
import libs.Subject;

/**
 *  Main application frame
 * @author Dejvino
 */
public class MainFrame extends javax.swing.JFrame {

    private Attendance attendance;

    private ExitAction exitAction;
    private AddStudentAction addStudentAction;
    private AddSubjectAction addSubjectAction;
    private AddSeminarAction addSeminarAction;
    private RemoveStudentAction removeStudentAction;
    private RemoveSeminarAction removeSeminarAction;
    private RegisterStudentAction registerStudentAction;
    private UnregisterStudentAction unregisterStudentAction;
    private ChangeLanguageAction changeLanguageAction;
    private DatabaseAction databaseAction;
    private MassAttendanceChangeAction massAttendanceChangeAction;
    private RemoveSubjectAction removeSubjectAction;
    private RemoveAllSeminarsAction removeAllSeminarsAction;

    private ButtonGroup languageButtons = new ButtonGroup();

    /** Creates new form MainFrame */
    public MainFrame() {
        Properties p = new Properties();
        try {
            p.load(new FileInputStream("settings.properties"));
            if(p.getProperty("locale") != null)
                Locale.setDefault(new Locale(p.getProperty("locale")));
            if (
                    p.getProperty("database.host") != null &&
                    p.getProperty("database.username") != null &&
                    p.getProperty("database.password") != null
               ) {
                attendance = new AttendanceImplDb(
                    p.getProperty("database.host"),
                    p.getProperty("database.username"),
                    p.getProperty("database.password")
                );
            }
            else {
                attendance = new AttendanceImplDb();
            }
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch(IllegalArgumentException ex) {
            JOptionPane.showConfirmDialog(null,
                java.util.ResourceBundle.getBundle("app/frames/language").getString("DatabaseConnErrorMSG"),
                java.util.ResourceBundle.getBundle("app/frames/language").getString("DatabaseConnError"),
                JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
        }

        // Adding actions
        exitAction = new ExitAction(this);
        addStudentAction = new AddStudentAction(this, attendance);
        addSubjectAction = new AddSubjectAction(this, attendance);
        addSeminarAction = new AddSeminarAction(this, attendance);
        removeStudentAction = new RemoveStudentAction(attendance);
        removeSeminarAction = new RemoveSeminarAction(attendance);
        registerStudentAction = new RegisterStudentAction(this, attendance);
        unregisterStudentAction = new UnregisterStudentAction(attendance);
        changeLanguageAction = new ChangeLanguageAction();
        databaseAction = new DatabaseAction(this);
        massAttendanceChangeAction = new MassAttendanceChangeAction(this, attendance);
        removeSubjectAction = new RemoveSubjectAction(attendance);
        removeAllSeminarsAction = new RemoveAllSeminarsAction(attendance);

        initComponents();
        
        languageButtons.clearSelection();

        ((JDate)jEntryDate).setDate(new Date());

        addSubjectAction.setTree(jSubjectsTree);
        addSeminarAction.setTree(jSubjectsTree);
        addStudentAction.setTree(jSubjectsTree);
        registerStudentAction.setTree(jSubjectsTree);
        unregisterStudentAction.setTree(jSubjectsTree);
        removeStudentAction.setTree(jSubjectsTree);
        removeSeminarAction.setTree(jSubjectsTree);
        massAttendanceChangeAction.setTree(jSubjectsTree);
        removeSubjectAction.setTree(jSubjectsTree);
        removeAllSeminarsAction.setTree(jSubjectsTree);

        // default action status
        addSeminarAction.setEnabled(false);
        addStudentAction.setEnabled(false);
        removeStudentAction.setEnabled(false);
        removeSeminarAction.setEnabled(false);
        registerStudentAction.setEnabled(false);
        unregisterStudentAction.setEnabled(false);
        massAttendanceChangeAction.setEnabled(false);
        removeSubjectAction.setEnabled(false);
        removeAllSeminarsAction.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jAddSubject = new javax.swing.JButton();
        jAddSeminar = new javax.swing.JButton();
        jRemoveSeminar = new javax.swing.JButton();
        jAddStudent = new javax.swing.JButton();
        jRegisterStudent = new javax.swing.JButton();
        jUnregisterStudent = new javax.swing.JButton();
        jRemoveStudent = new javax.swing.JButton();
        jMassAttendanceChange = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jSubjectsTree = new javax.swing.JTree();
        jScrollPane2 = new javax.swing.JScrollPane();
        jStudentAttendance = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jStudentName = new javax.swing.JTextField();
        jStudentID = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jSubjectName = new javax.swing.JTextField();
        jSeminarNumber = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jSubjectCode = new javax.swing.JTextField();
        jDeleteEntry = new javax.swing.JButton();
        jAddEntry = new javax.swing.JButton();
        jEntryAttendance = new javax.swing.JCheckBox();
        jEntryDate = new JDate();
        jSeparator10 = new javax.swing.JSeparator();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenuFile = new javax.swing.JMenu();
        jMenuExit = new javax.swing.JMenuItem();
        jMenuSubject = new javax.swing.JMenu();
        jMenuAddSubject = new javax.swing.JMenuItem();
        jMenuRemoveSubject = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jMenuRemoveAllSeminars = new javax.swing.JMenuItem();
        jMenuSeminar = new javax.swing.JMenu();
        jMenuAddSeminar = new javax.swing.JMenuItem();
        jMenuRemoveSeminar = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuItem14 = new javax.swing.JMenuItem();
        jMenuRemoveStudentsRegistration = new javax.swing.JMenuItem();
        jSeparator8 = new javax.swing.JSeparator();
        jMenuMassAttendanceChange = new javax.swing.JMenuItem();
        jMenuStudent = new javax.swing.JMenu();
        jMenuAddStudent = new javax.swing.JMenuItem();
        jMenuRemoveStudent = new javax.swing.JMenuItem();
        jMenuSetup = new javax.swing.JMenu();
        jMenuLanguage = new javax.swing.JMenu();
        jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem2 = new javax.swing.JRadioButtonMenuItem();
        jMenuDatabase = new javax.swing.JMenuItem();
        jMenuHelp = new javax.swing.JMenu();
        jMenuStartHelp = new javax.swing.JMenuItem();
        jMenuAboutApp = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("app/frames/language"); // NOI18N
        setTitle(bundle.getString("MainWindow")); // NOI18N
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jToolBar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar1.setRollover(true);

        jAddSubject.setAction(addSubjectAction);
        jAddSubject.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/gui/new_book.png"))); // NOI18N
        jAddSubject.setToolTipText(bundle.getString("AddSubject")); // NOI18N
        jAddSubject.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jAddSubject.setFocusable(false);
        jAddSubject.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jAddSubject.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jAddSubject);

        jAddSeminar.setAction(addSeminarAction);
        jAddSeminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/gui/new_people.png"))); // NOI18N
        jAddSeminar.setToolTipText(bundle.getString("AddSeminar")); // NOI18N
        jAddSeminar.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jAddSeminar.setFocusable(false);
        jAddSeminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jAddSeminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jAddSeminar);

        jRemoveSeminar.setAction(removeSeminarAction);
        jRemoveSeminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/gui/remove_people.png"))); // NOI18N
        jRemoveSeminar.setToolTipText(bundle.getString("RemoveSeminar")); // NOI18N
        jRemoveSeminar.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jRemoveSeminar.setFocusable(false);
        jRemoveSeminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRemoveSeminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jRemoveSeminar);

        jAddStudent.setAction(addStudentAction);
        jAddStudent.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/gui/new_person.png"))); // NOI18N
        jAddStudent.setToolTipText(bundle.getString("AddStudent")); // NOI18N
        jAddStudent.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jAddStudent.setFocusable(false);
        jAddStudent.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jAddStudent.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jAddStudent);

        jRegisterStudent.setAction(registerStudentAction);
        jRegisterStudent.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/gui/person_plus.png"))); // NOI18N
        jRegisterStudent.setToolTipText(bundle.getString("RegisterStudent")); // NOI18N
        jRegisterStudent.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jRegisterStudent.setFocusable(false);
        jRegisterStudent.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRegisterStudent.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jRegisterStudent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRegisterStudentActionPerformed(evt);
            }
        });
        jToolBar1.add(jRegisterStudent);

        jUnregisterStudent.setAction(unregisterStudentAction);
        jUnregisterStudent.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/gui/person_minus.png"))); // NOI18N
        jUnregisterStudent.setToolTipText(bundle.getString("RemoveStudentsRegistration")); // NOI18N
        jUnregisterStudent.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jUnregisterStudent.setFocusable(false);
        jUnregisterStudent.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jUnregisterStudent.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jUnregisterStudent);

        jRemoveStudent.setAction(removeStudentAction);
        jRemoveStudent.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/gui/person_remove.png"))); // NOI18N
        jRemoveStudent.setToolTipText(bundle.getString("RemoveStudent")); // NOI18N
        jRemoveStudent.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jRemoveStudent.setFocusable(false);
        jRemoveStudent.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRemoveStudent.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jRemoveStudent);

        jMassAttendanceChange.setAction(massAttendanceChangeAction);
        jMassAttendanceChange.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/gui/people_check.png"))); // NOI18N
        jMassAttendanceChange.setToolTipText(bundle.getString("MassAttendanceChange")); // NOI18N
        jMassAttendanceChange.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jMassAttendanceChange.setFocusable(false);
        jMassAttendanceChange.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jMassAttendanceChange.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jMassAttendanceChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMassAttendanceChangeActionPerformed(evt);
            }
        });
        jToolBar1.add(jMassAttendanceChange);

        jSubjectsTree.setModel(new SubjectTreeModel(attendance));
        jSubjectsTree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener() {
            public void treeWillCollapse(javax.swing.event.TreeExpansionEvent evt)throws javax.swing.tree.ExpandVetoException {
            }
            public void treeWillExpand(javax.swing.event.TreeExpansionEvent evt)throws javax.swing.tree.ExpandVetoException {
                jSubjectsTreeTreeWillExpand(evt);
            }
        });
        jSubjectsTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jSubjectsTreeValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jSubjectsTree);

        jStudentAttendance.setModel(new AttendanceTableModel(attendance));
        jStudentAttendance.setCellSelectionEnabled(true);
        jScrollPane2.setViewportView(jStudentAttendance);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(bundle.getString("Student")); // NOI18N

        jLabel2.setText("Jméno");

        jLabel3.setText("ID");

        jStudentName.setEditable(false);

        jStudentID.setEditable(false);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText(bundle.getString("Subject")); // NOI18N

        jSubjectName.setEditable(false);

        jSeminarNumber.setEditable(false);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText(bundle.getString("Seminar")); // NOI18N

        jSubjectCode.setEditable(false);

        jDeleteEntry.setText(bundle.getString("Delete")); // NOI18N
        jDeleteEntry.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jDeleteEntryActionPerformed(evt);
            }
        });

        jAddEntry.setText(bundle.getString("Add")); // NOI18N
        jAddEntry.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAddEntryActionPerformed(evt);
            }
        });

        jEntryAttendance.setSelected(true);
        jEntryAttendance.setText(bundle.getString("Attendance")); // NOI18N

        jEntryDate.setText("13.05.2004");

        jSeparator10.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jMenuFile.setText(bundle.getString("File")); // NOI18N

        jMenuExit.setAction(exitAction);
        jMenuExit.setText(bundle.getString("Exit")); // NOI18N
        jMenuFile.add(jMenuExit);

        jMenuBar1.add(jMenuFile);

        jMenuSubject.setText(bundle.getString("Subject")); // NOI18N

        jMenuAddSubject.setAction(addSubjectAction);
        jMenuAddSubject.setText(bundle.getString("AddSubject")); // NOI18N
        jMenuSubject.add(jMenuAddSubject);

        jMenuRemoveSubject.setAction(removeSubjectAction);
        jMenuRemoveSubject.setText(bundle.getString("RemoveSubject")); // NOI18N
        jMenuSubject.add(jMenuRemoveSubject);
        jMenuSubject.add(jSeparator2);

        jMenuRemoveAllSeminars.setAction(removeAllSeminarsAction);
        jMenuRemoveAllSeminars.setText(bundle.getString("RemoveAllSeminars")); // NOI18N
        jMenuSubject.add(jMenuRemoveAllSeminars);

        jMenuBar1.add(jMenuSubject);

        jMenuSeminar.setText(bundle.getString("Seminar")); // NOI18N

        jMenuAddSeminar.setAction(addSeminarAction);
        jMenuAddSeminar.setText(bundle.getString("AddSeminar")); // NOI18N
        jMenuSeminar.add(jMenuAddSeminar);

        jMenuRemoveSeminar.setAction(removeSeminarAction);
        jMenuRemoveSeminar.setText(bundle.getString("RemoveSeminar")); // NOI18N
        jMenuSeminar.add(jMenuRemoveSeminar);
        jMenuSeminar.add(jSeparator1);

        jMenuItem14.setAction(registerStudentAction);
        jMenuItem14.setText(bundle.getString("RegisterStudent")); // NOI18N
        jMenuSeminar.add(jMenuItem14);

        jMenuRemoveStudentsRegistration.setAction(unregisterStudentAction);
        jMenuRemoveStudentsRegistration.setText(bundle.getString("RemoveStudentsRegistration")); // NOI18N
        jMenuSeminar.add(jMenuRemoveStudentsRegistration);
        jMenuSeminar.add(jSeparator8);

        jMenuMassAttendanceChange.setAction(massAttendanceChangeAction);
        jMenuMassAttendanceChange.setText(bundle.getString("MassAttendanceChange")); // NOI18N
        jMenuSeminar.add(jMenuMassAttendanceChange);

        jMenuBar1.add(jMenuSeminar);

        jMenuStudent.setText(bundle.getString("Student")); // NOI18N

        jMenuAddStudent.setAction(addStudentAction);
        jMenuAddStudent.setText(bundle.getString("AddStudent")); // NOI18N
        jMenuStudent.add(jMenuAddStudent);

        jMenuRemoveStudent.setAction(removeStudentAction);
        jMenuRemoveStudent.setText(bundle.getString("RemoveStudent")); // NOI18N
        jMenuStudent.add(jMenuRemoveStudent);

        jMenuBar1.add(jMenuStudent);

        jMenuSetup.setText(bundle.getString("Setup")); // NOI18N

        jMenuLanguage.setText(bundle.getString("Language")); // NOI18N

        jRadioButtonMenuItem1.setAction(changeLanguageAction);
        languageButtons.add(jRadioButtonMenuItem1);
        jRadioButtonMenuItem1.setSelected(true);
        jRadioButtonMenuItem1.setText("Česky");
        jRadioButtonMenuItem1.setActionCommand("cs_CZ");
        jMenuLanguage.add(jRadioButtonMenuItem1);

        jRadioButtonMenuItem2.setAction(changeLanguageAction);
        languageButtons.add(jRadioButtonMenuItem2);
        jRadioButtonMenuItem2.setSelected(true);
        jRadioButtonMenuItem2.setText("English");
        jRadioButtonMenuItem2.setActionCommand("en_US");
        jMenuLanguage.add(jRadioButtonMenuItem2);

        jMenuSetup.add(jMenuLanguage);

        jMenuDatabase.setAction(databaseAction);
        jMenuDatabase.setText(bundle.getString("Database")); // NOI18N
        jMenuSetup.add(jMenuDatabase);

        jMenuBar1.add(jMenuSetup);

        jMenuHelp.setText(bundle.getString("Help")); // NOI18N

        jMenuStartHelp.setText(bundle.getString("Help")); // NOI18N
        jMenuStartHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuStartHelpActionPerformed(evt);
            }
        });
        jMenuHelp.add(jMenuStartHelp);

        jMenuAboutApp.setText(bundle.getString("AboutApplication")); // NOI18N
        jMenuAboutApp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuAboutAppActionPerformed(evt);
            }
        });
        jMenuHelp.add(jMenuAboutApp);

        jMenuBar1.add(jMenuHelp);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jStudentID, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
                            .addComponent(jStudentName, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jSubjectName, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jSubjectCode, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jSeminarNumber)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jEntryDate, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jEntryAttendance)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jAddEntry)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jDeleteEntry)))
                .addContainerGap())
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 563, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jStudentName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jStudentID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jSubjectName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSeminarNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSubjectCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jEntryDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jAddEntry, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jEntryAttendance, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jDeleteEntry, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jSeparator10, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                        .addContainerGap())
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jRegisterStudentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRegisterStudentActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jRegisterStudentActionPerformed

    private void jMassAttendanceChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMassAttendanceChangeActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jMassAttendanceChangeActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        exitAction.actionPerformed(new ActionEvent(this, evt.getID(), evt.paramString()));
    }//GEN-LAST:event_formWindowClosing

    private void jSubjectsTreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jSubjectsTreeValueChanged
        TreePath path = evt.getPath();

        // Subject
        if(path.getPathCount() == 2)
        {
            removeSubjectAction.setEnabled(true);
            removeAllSeminarsAction.setEnabled(true);
            addSeminarAction.setEnabled(true);
        } else {
            addSeminarAction.setEnabled(false);
            removeSubjectAction.setEnabled(false);
            
            removeAllSeminarsAction.setEnabled(false);
        }
        // Seminar
        if(path.getPathCount() == 3)
        {
            registerStudentAction.setEnabled(true);
            addStudentAction.setEnabled(true);
            removeSeminarAction.setEnabled(true);
            massAttendanceChangeAction.setEnabled(true);
        } else {
            registerStudentAction.setEnabled(false);
            addStudentAction.setEnabled(false);
            removeSeminarAction.setEnabled(false);
            massAttendanceChangeAction.setEnabled(false);
        }
        // Student
        if(path.getPathCount() == 4)
        {
            Subject subject = (Subject)path.getPathComponent(1);
            Seminar seminar = (Seminar)path.getPathComponent(2);
            Student student = (Student)path.getPathComponent(3);
            jSubjectName.setText(subject.getName());
            jSubjectCode.setText(subject.getCode());
            jSeminarNumber.setText(Integer.toString(seminar.getNumber()));
            jStudentName.setText(student.getName());
            jStudentID.setText(Integer.toString(student.getId()));
            AttendanceTableModel tableModel = (AttendanceTableModel)jStudentAttendance.getModel();
            tableModel.setStudent(student);
            tableModel.setSeminar(seminar);
            tableModel.fireTableDataChanged();

            unregisterStudentAction.setEnabled(true);
            removeStudentAction.setEnabled(true);
        } else {
            jSubjectName.setText("");
            jSubjectCode.setText("");
            jSeminarNumber.setText("");
            jStudentName.setText("");
            jStudentID.setText("");
            AttendanceTableModel tableModel = (AttendanceTableModel)jStudentAttendance.getModel();
            tableModel.setStudent(null);
            tableModel.setSeminar(null);
            tableModel.fireTableDataChanged();

            unregisterStudentAction.setEnabled(false);
            removeStudentAction.setEnabled(false);
        }
    }//GEN-LAST:event_jSubjectsTreeValueChanged

    private void jSubjectsTreeTreeWillExpand(javax.swing.event.TreeExpansionEvent evt)throws javax.swing.tree.ExpandVetoException {//GEN-FIRST:event_jSubjectsTreeTreeWillExpand
        jSubjectsTree.getModel().valueForPathChanged(evt.getPath(), null);
    }//GEN-LAST:event_jSubjectsTreeTreeWillExpand

    private void jDeleteEntryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDeleteEntryActionPerformed
        int row = jStudentAttendance.getSelectedRow();
        if(row < 0)
            return;
        AttendanceTableModel model = (AttendanceTableModel)jStudentAttendance.getModel();
        AttendanceEntry entry = new AttendanceEntry((Date)model.getValueAt(row, 0),
                ((Boolean)model.getValueAt(row, 1)).booleanValue());
        try {
            attendance.removeAttendance(model.getSeminar(), model.getStudent(), entry);
        } catch (AttendanceException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        model.fireTableRowsDeleted(row, row);
    }//GEN-LAST:event_jDeleteEntryActionPerformed

    private void jAddEntryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAddEntryActionPerformed
        AttendanceTableModel model = (AttendanceTableModel)jStudentAttendance.getModel();
        Seminar seminar = model.getSeminar();
        Student student = model.getStudent();
        if(seminar == null || student == null)
            return;
        Date date = ((JDate)jEntryDate).getDate();
        AttendanceEntry entry = new AttendanceEntry(date, jEntryAttendance.isSelected());
        
        try {
            attendance.addAttendance(seminar, student, entry);
            model.fireTableRowsInserted(model.getRowCount(), 0);
        } catch (AttendanceException ex) {
            JOptionPane.showConfirmDialog(null,
                java.util.ResourceBundle.getBundle("app/frames/language").getString("AttendanceExistsMSG"),
                java.util.ResourceBundle.getBundle("app/frames/language").getString("AttendanceExists"),
                JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE
            );
        }
    }//GEN-LAST:event_jAddEntryActionPerformed

    private void jMenuStartHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuStartHelpActionPerformed
        try {
            Desktop.getDesktop().browse(new URI("http://google.com/search?q=attendance"));
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (URISyntaxException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_jMenuStartHelpActionPerformed

    private void jMenuAboutAppActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuAboutAppActionPerformed
        new AboutForm().setVisible(true);
}//GEN-LAST:event_jMenuAboutAppActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAddEntry;
    private javax.swing.JButton jAddSeminar;
    private javax.swing.JButton jAddStudent;
    private javax.swing.JButton jAddSubject;
    private javax.swing.JButton jDeleteEntry;
    private javax.swing.JCheckBox jEntryAttendance;
    private javax.swing.JFormattedTextField jEntryDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JButton jMassAttendanceChange;
    private javax.swing.JMenuItem jMenuAboutApp;
    private javax.swing.JMenuItem jMenuAddSeminar;
    private javax.swing.JMenuItem jMenuAddStudent;
    private javax.swing.JMenuItem jMenuAddSubject;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuDatabase;
    private javax.swing.JMenuItem jMenuExit;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenu jMenuHelp;
    private javax.swing.JMenuItem jMenuItem14;
    private javax.swing.JMenu jMenuLanguage;
    private javax.swing.JMenuItem jMenuMassAttendanceChange;
    private javax.swing.JMenuItem jMenuRemoveAllSeminars;
    private javax.swing.JMenuItem jMenuRemoveSeminar;
    private javax.swing.JMenuItem jMenuRemoveStudent;
    private javax.swing.JMenuItem jMenuRemoveStudentsRegistration;
    private javax.swing.JMenuItem jMenuRemoveSubject;
    private javax.swing.JMenu jMenuSeminar;
    private javax.swing.JMenu jMenuSetup;
    private javax.swing.JMenuItem jMenuStartHelp;
    private javax.swing.JMenu jMenuStudent;
    private javax.swing.JMenu jMenuSubject;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem2;
    private javax.swing.JButton jRegisterStudent;
    private javax.swing.JButton jRemoveSeminar;
    private javax.swing.JButton jRemoveStudent;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jSeminarNumber;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator10;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator8;
    private javax.swing.JTable jStudentAttendance;
    private javax.swing.JTextField jStudentID;
    private javax.swing.JTextField jStudentName;
    private javax.swing.JTextField jSubjectCode;
    private javax.swing.JTextField jSubjectName;
    private javax.swing.JTree jSubjectsTree;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton jUnregisterStudent;
    // End of variables declaration//GEN-END:variables

}
